package com.fairytask.ui.main
{
	
	import com.fairytask.data.ViewObject;
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.event.LogicEvent;
	import com.fairytask.objects.Player;
	import com.fairytask.tools.MoneyTools;
	import com.fairytask.ui.base.FrameUI;
	import com.greensock.TimelineMax;
	import com.greensock.TweenAlign;
	import com.greensock.TweenMax;
	import com.greensock.easing.Linear;
	
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * 主界面人物头像 
	 * @author xujh
	 * 
	 */
	public class HeadUI extends FrameUI
	{
		private static var _self		:HeadUI;
		
		public var headBG				:Clip;
		public var playerPhoto   		:Clip;
		public var nameLab				:Label;
		//vip按钮
		public var vipBtn				:Button;
		//充值按钮
		public var payBtn				:Button;
		public var silverBtn			:Button;	//加银两
		public var goldBtn				:Button;	//加仙玉
		public var spBtn               :Button;    //加精力
		//
		public var moneyLab			:Label;		//银两
		public var goldLab				:Label;		//仙玉
		public var spiritLab			:Label;		//灵
		public var spLab				:Label;		//精力
		//图片文字XX:
		public var moneyMc		:Clip;
		public var goldMc		:Clip;
		public var spiritMc	:Clip;
		public var spMc		:Clip;
		//底图
		public var moneyBG		:MovieClip;
		public var goldBG		:MovieClip;
		public var spiritBG	:MovieClip;
		public var moneyLine	:MovieClip;
		public var goldLine	:MovieClip;
		public var spiritLine	:MovieClip;
		public var spBar		:MovieClip;
		//图标
		public var moneyIcon	:Clip;
		public var goldIcon	:Clip;
		public var spiritIcon	:Clip;
		
		//==========================================================
		private var _player			:Player;
		private var _playerName		:String;//人物名称
		
		private var _arrBindProp		:Array=[];
		private var _data				:ViewObject;
		
		private var _vipLv		:int;	//VIP 等级
		private var _buyCount	:int;	//购买次数
		private var _timeLine	:TimelineMax;
		private var _maxSP		:int;
		
		public static function getInstance():HeadUI
		{
			if(_self == null)
			{
				_self	=	new HeadUI();				
			}
			return _self;
		}
		
		public function HeadUI(popupUI:Boolean=false)
		{
			super(popupUI);
			//_player         =   Player.getInstance();
			
			playerPhoto.buttonMode = true;
			
			init();
			initEvent();
			initBind();
		}
		
		/**
		 *初始化血条透明层 
		 * 
		 */		
		private function init():void
		{
			//监听玩家准备完毕
			//model.addEventListener(LogicEvent.USERLOADED, onUserReady);
			
			_timeLine = new TimelineMax({repeat:-1});
			_timeLine.appendMultiple([TweenMax.to(vipBtn, 0.2, {glowFilter:{color:0xFF0000, alpha:1, blurX:30, blurY:30,strength:1.5}, ease:Linear.easeNone}),
				TweenMax.to(vipBtn, 0.1, {glowFilter:{color:0xFF0000, alpha:1, blurX:0, blurY:0}, ease:Linear.easeNone})],0,TweenAlign.SEQUENCE);
			_timeLine.stop();
			
			
			//默认vip按钮显示
			vipBtn.label	=	"VIP";
			
			//临时按钮禁用
			vipBtn.enabled = false;
			payBtn.enabled = false;
			silverBtn.enabled = false;
			goldBtn.enabled = false;
			//spBtn.enabled = false;
		}
		
		/**
		 * 初始化事件
		 * 
		 */		
		private function initEvent():void
		{
			vipBtn.addEventListener(MouseEvent.CLICK,onVipShow);
			payBtn.addEventListener(MouseEvent.CLICK,onPay);
			silverBtn.addEventListener(MouseEvent.CLICK,onSilverAdd);
			goldBtn.addEventListener(MouseEvent.CLICK,onGoldAdd);
			spBtn.addEventListener(MouseEvent.CLICK,onSpAdd);
		}
		
		private function initBind():void
		{
			/*bindManager.BindViewProp(model.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"Silver",setSilver);
			bindManager.BindViewProp(model.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"Gold",setGold);
			bindManager.BindViewProp(model.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"Spirit",setSpirit);*/
		}
		
		/**
		 *	绘制矩形 
		 * @param graphic
		 * @param width
		 * @param int
		 * 
		 */		
		private function drawRect(graphic:Graphics,width:int,height:int):void
		{
			graphic.clear();
			graphic.beginFill(0,0);
			graphic.drawRect(0,0,width,height);
			graphic.endFill();
		}
		
		/**
		 *显示vip界面 
		 * 
		 */
		private function onVipShow(e:MouseEvent):void
		{
			_timeLine.stop();
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ASK_VIP_LEVEL);
//			VipUI.getInstance().show();
		}
		
		/**
		 * 点击充值按钮 
		 * @param e
		 * 
		 */
		private function onPay(e:MouseEvent):void
		{
			
		}
		
		/**
		 * 点击增加银两 
		 * @param e
		 * 
		 */
		private function onSilverAdd(e:MouseEvent):void
		{
			
		}
		
		/**
		 * 点击增加仙玉 
		 * @param e
		 * 
		 */
		private function onGoldAdd(e:MouseEvent):void
		{
			
		}
		
		/**
		 * 点击增加精力
		 * @param e
		 * 
		 */
		private function onSpAdd(e:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CLICK_SP_TRADE);
		}
		/**
		 *   设置按钮   不可用   FOR  新手引导  
		 * @param component
		 * 
		 */		
		public function openClose(component:String,isOpen:Boolean):void
		{
			var index:int=component.search("_");
			var bin:String=component.substr(0,index);
			var mc:String=component.substring(index+1);
			
			//			this[bin].photo.mouseEnabled	=	false;
			if(index	!=	-1)
			{
				
				if(this[bin].photo is Button)
				{
					this[bin].photo.enabled	=	isOpen;
				}
				else
				{
					this[bin].photo.mouseEnabled	=	isOpen;
					this[bin].photo.mouseChildren	=	isOpen;
				}
			}
			else
			{
				if(this[component] is Button)
				{
					this[component].enabled	=	isOpen;
				}
				else
				{
					this[component].mouseEnabled	=	isOpen;
				}
				
			}
		}
		
		/**
		 * 绑定玩家数据 
		 */
		private function onUserReady(evt:LogicEvent):void
		{
			/*bindManager.BindProp(_player.data,"Model",this,"pPhoto");
			//姓名
			bindManager.BindProp(_player.data, "Name",this,"rName");
			//VIP
			bindManager.BindProp(_player.data, "VipLevel",this,"rVip");
			bindManager.BindProp(_player.data, "BuySTCount",this,"setStCount");
			bindManager.BindProp(_player.data, "MaxSP",this,"rMaxSP");
			bindManager.BindProp(_player.data, "SP",this,"rSP");*/
		}
		
		public function set setStCount(value:int):void
		{
			_buyCount	=	value;	
		}
		
		private function playerPhotoClick(evt:MouseEvent):void
		{
			
		}
		
		//========================================================================
		//自动生成的数据处理
		/**
		 * 
		 *人物头像
		 * 
		 */		
		public function set pPhoto(val:String):void
		{
			playerPhoto.source			=	ResConfig.pngPhotoPath + val + "_b.png";
			playerPhoto.x = 20;
			playerPhoto.y = 28;
			
		}
		//========================================================================
		/**当前人物姓名*/
		public function set rName(val:String):void
		{
			_playerName = val;
			nameLab.htmlStrByBorder = _playerName;
//			nameLab.x = 130 + (80 - nameLab.width)/2;
		}
		/**VIP*/
		public function set rVip(val:int):void
		{
			_vipLv	=	val;
			if(val!=0)
			{
				vipBtn.label	=	"VIP"+val;
				vipBtn.enabled  = true;
			}
			else
			{
				vipBtn.label	=	"VIP";
			}
		}
		
		public function get vipLevel():int
		{
			return _vipLv;
		}
		
		/**
		 * 最大精力 
		 * @param val
		 * 
		 */
		public function set rMaxSP(val:int):void
		{
			_maxSP = val;
		}
		
		/**
		 * 精力 
		 * @param val
		 * 
		 */
		public function set rSP(val:int):void
		{
			spLab.htmlStrByBorder = val + "/" + _maxSP;
			spLab.x = 110 + (115 - spLab.width)/2;
			if(val > _maxSP)
			{
				val = _maxSP;
			}
			spBar.maskBar.width = (val/_maxSP)*spBar.BarBg.width;
		}
		
		/**
		 * 银两 
		 * @param key
		 * @param val
		 * 
		 */
		private function setSilver(key:String,val:int):void
		{
			setMoneyLab(moneyLab,val);
		}
		
		/**
		 * 仙玉 
		 * @param key
		 * @param val
		 * 
		 */
		private function setGold(key:String,val:int):void
		{
			setMoneyLab(goldLab,val);
		}
		
		/**
		 * 设置灵气
		 * @param key
		 * @param val
		 * 
		 */
		private function setSpirit(key:String,val:int):void
		{
			setMoneyLab(spiritLab,val);
		}
		
		/**
		 * 设置钱和位置的显示 
		 * @param lab
		 * @param val
		 * 
		 */
		private function setMoneyLab(lab:Label,val:int):void
		{
			lab.htmlStrByBorder = MoneyTools.setMoney(val);
			lab.x = 115 + (95 - lab.width)/2;
		}
		//========================================================================
		
		/**
		 * 重写显示 
		 * @param space
		 * @param onshow
		 * @param argc
		 * 
		 */
		override public function show(space:String=BACKLAYER, onshow:Function=null):void
		{
			super.show(space,onshow);
		}
		
		/**
		 * 场景变化改变位置 
		 * 
		 */
		override protected function stageReSize(evt:Event=null):void
		{
			this.x	=	initX - GameStage.mainLayer.x;
			this.y = 	initY - GameStage.mainLayer.y;
		}
	}
}